package com.ycu.jk16.mall.controller.admin;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ycu.jk16.mall.domain.Order;
import com.ycu.jk16.mall.domain.OrderDetail;
import com.ycu.jk16.mall.domain.Shipping;
import com.ycu.jk16.mall.service.OrderService;
import com.ycu.jk16.mall.util.assistdata.AssistOrder;
import com.ycu.jk16.mall.util.assistdata.AssistOrderItem;
import com.ycu.jk16.mall.util.assistdata.AssistShipping;
import com.ycu.jk16.mall.util.common.AndroidMalConstant;
import com.ycu.jk16.mall.util.common.PageUtil;
import com.ycu.jk16.mall.util.common.ResponseJsonUtils;

/**
 * 
 * @ClassName AdminOrderController
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author 倪宗龙
 * @date 2018年12月18日 下午2:35:33
 * @version V1.0
 */
@Controller
@RequestMapping(value = "/manage/order")
public class AdminOrderController {
	@Autowired
	@Qualifier("orderService")
	private OrderService orderService;

	/**
	 * 
	 * @Title getOrderList  
	 * @Description TODO(获取订单列表)  
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @param response
	 * @return void
	 * @throws
	 */
	@RequestMapping(value = "/list")
	public void getOrderList(@RequestParam(value="pageNum", defaultValue="1")int pageNum,@RequestParam(value="pageSize", defaultValue="10") int pageSize, HttpServletResponse response) {
		Map<String, Object> orderListData = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		// 添加params参数
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);

		// order的集合
		List<Order> orderList;
		List<OrderDetail> orderDetailList;
		List<AssistOrder> assistOrders = new ArrayList<>();
		List<AssistOrderItem> assistOrderItems = new ArrayList<AssistOrderItem>();

		try {
			// 测试catch块
			orderList = orderService.getOrdersWithParams(params);
			// page分页
			PageUtil page = new PageUtil(pageNum, pageSize, orderList.size());

			// 加入json数据
			orderListData.put("status", 0);

			data.put("pageNum", pageNum);
			data.put("pageSize", pageSize);
			data.put("size", orderList.size());
			data.put("total", page.getRecordCount());
			data.put("pages", page.getTotalPage());
			// list
			for (int i = 0; i < orderList.size(); i++) {
				orderDetailList = orderService.findOrderDetailListByNo(orderList.get(i).getNo());
				// orderItemVoList
				for (int j = 0; j < orderDetailList.size(); j++) {
					OrderDetail orderDetail = orderDetailList.get(j);
					AssistOrderItem assistOrderItem = new AssistOrderItem();
					assistOrderItem.setOrderNo(orderDetail.getOrder().getNo());
					assistOrderItem.setProductId(orderDetail.getProduct().getId());
					assistOrderItem.setSubTitle(orderDetail.getProduct().getSubtitle());
					assistOrderItem.setProductName(orderDetail.getCurrentProductName());
					assistOrderItem.setProductImage(orderDetail.getProduct().getMainImage());
					assistOrderItem.setCurrentUnitPrice(orderDetail.getCurrentUnitPrice());
					assistOrderItem.setQuantity(orderDetail.getQuantity());
					assistOrderItem.setTotalPrice(BigDecimal
							.valueOf(orderDetail.getQuantity() * orderDetail.getCurrentUnitPrice().doubleValue()));
					assistOrderItem.setCreateTime(orderDetail.getOrder().getCreateTime());
					// 添加订单详情，组成list
					assistOrderItems.add(assistOrderItem);
				}

				Order order = orderList.get(i);
				AssistOrder assistOrder = new AssistOrder();
				assistOrder.setOrderNo(order.getNo());
				assistOrder.setPayment(order.getPayment());
				assistOrder.setPaymentTime(order.getCreateTime());
				assistOrder.setSendTime(order.getCreateTime());
				assistOrder.setEndTime(order.getEndTime());
				assistOrder.setCloseTime(order.getCloseTime());
				assistOrder.setCreateTime(order.getCreateTime());
				assistOrder.setImageHost(AndroidMalConstant.HOST_ADDRESS);
				assistOrder.setShippingId(order.getShipping().getId());
				assistOrder.setReceiverName(order.getShipping().getName());
				assistOrder.setOrderItems(assistOrderItems);

				// 添加assistOrder
				assistOrders.add(assistOrder);
			}
			data.put("list", assistOrders);
			orderListData.put("data", data);
			orderListData.put("firstPage", 1);
			orderListData.put("prePage", page.hasPreviousPage(pageNum) ? pageNum - 1 : pageNum);
			orderListData.put("nextPage", page.hasNextPage(pageNum) ? pageNum + 1 : pageNum);
			orderListData.put("lastPage", page.getTotalPage());
			orderListData.put("isFisrtPage", page.isFirstPage(pageNum));
			orderListData.put("isLastPage", page.isLastPage(pageNum));
			orderListData.put("hasPreviousPage", page.hasPreviousPage(pageNum));
			orderListData.put("hasNextPage", page.hasNextPage(pageNum));
		} catch (NullPointerException e) {
			e.printStackTrace();
			orderListData.put("status", 1);
			orderListData.put("msg", "没登录");
		} finally {
			ResponseJsonUtils.json(response, orderListData);
		}
	}

	/**
	 * 
	 * @Title search  
	 * @Description TODO(搜索订单)  
	 * @param @param orderNo
	 * @param @param response
	 * @return void
	 * @throws
	 */
	@RequestMapping(value = "/search")
	public void search(String orderNo, HttpServletResponse response) {
		Map<String, Object> orderListData = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		// 添加params参数
		int pageNum = 1, pageSize = 100;
		params.put("no", orderNo);
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);

		// order的集合
		List<Order> orderList;
		List<OrderDetail> orderDetailList;
		List<AssistOrder> assistOrders = new ArrayList<>();
		List<AssistOrderItem> assistOrderItems = new ArrayList<AssistOrderItem>();

		try {
			// 测试catch块
			orderList = orderService.getOrdersWithParams(params);

			// page分页
			PageUtil page = new PageUtil(pageNum, pageSize, orderList.size());

			// 加入json数据
			orderListData.put("status", 0);

			data.put("pageNum", pageNum);
			data.put("pageSize", pageSize);
			data.put("size", orderList.size());
			data.put("total", page.getRecordCount());
			data.put("pages", page.getTotalPage());
			// list
			for (int i = 0; i < orderList.size(); i++) {
				orderDetailList = orderService.findOrderDetailListByNo(orderList.get(i).getNo());
				// orderItemVoList
				for (int j = 0; j < orderDetailList.size(); j++) {
					OrderDetail orderDetail = orderDetailList.get(j);
					AssistOrderItem assistOrderItem = new AssistOrderItem();
					assistOrderItem.setOrderNo(orderDetail.getOrder().getNo());
					assistOrderItem.setProductId(orderDetail.getProduct().getId());
					assistOrderItem.setSubTitle(orderDetail.getProduct().getSubtitle());
					assistOrderItem.setProductName(orderDetail.getCurrentProductName());
					assistOrderItem.setProductImage(orderDetail.getProduct().getMainImage());
					assistOrderItem.setCurrentUnitPrice(orderDetail.getCurrentUnitPrice());
					assistOrderItem.setQuantity(orderDetail.getQuantity());
					assistOrderItem.setTotalPrice(BigDecimal
							.valueOf(orderDetail.getQuantity() * orderDetail.getCurrentUnitPrice().doubleValue()));
					assistOrderItem.setCreateTime(orderDetail.getOrder().getCreateTime());
					// 添加订单详情，组成list
					assistOrderItems.add(assistOrderItem);
				}

				Order order = orderList.get(i);
				AssistOrder assistOrder = new AssistOrder();
				assistOrder.setOrderNo(order.getNo());
				assistOrder.setPayment(order.getPayment());
				assistOrder.setPaymentTime(order.getCreateTime());
				assistOrder.setSendTime(order.getCreateTime());
				assistOrder.setEndTime(order.getEndTime());
				assistOrder.setCloseTime(order.getCloseTime());
				assistOrder.setCreateTime(order.getCreateTime());
				assistOrder.setImageHost(orderDetailList.get(i).getProduct().getImageHost());
				assistOrder.setShippingId(order.getShipping().getId());
				assistOrder.setReceiverName(order.getShipping().getName());
				assistOrder.setOrderItems(assistOrderItems);

				// 添加assistOrder
				assistOrders.add(assistOrder);
			}
			data.put("list", assistOrders);
			orderListData.put("data", data);
			orderListData.put("firstPage", 1);
			orderListData.put("prePage", page.hasPreviousPage(pageNum) ? pageNum - 1 : pageNum);
			orderListData.put("nextPage", page.hasNextPage(pageNum) ? pageNum + 1 : pageNum);
			orderListData.put("lastPage", page.getTotalPage());
			orderListData.put("isFisrtPage", page.isFirstPage(pageNum));
			orderListData.put("isLastPage", page.isLastPage(pageNum));
			orderListData.put("hasPreviousPage", page.hasPreviousPage(pageNum));
			orderListData.put("hasNextPage", page.hasNextPage(pageNum));
		} catch (NullPointerException e) {
			e.printStackTrace();
			orderListData.put("status", 1);
			orderListData.put("msg", "没登录");
		} finally {
			ResponseJsonUtils.json(response, orderListData);
		}
	}

	/**
	 * 
	 * @Title detail  
	 * @Description TODO(订单详情)  
	 * @param @param orderNo
	 * @param @param response
	 * @return void
	 * @throws
	 */
	@RequestMapping(value = "/detail")
	public void detail(String orderNo, HttpServletResponse response) {
		Map<String, Object> orderListData = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		// 添加params参数
		int pageNum = 1, pageSize = 100;
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);

		// order的集合
		List<OrderDetail> orderDetailList;
		List<AssistOrderItem> assistOrderItems = new ArrayList<AssistOrderItem>();

		try {
			// 测试catch块

			// 加入json数据
			orderListData.put("status", 0);

			// list
			orderDetailList = orderService.findOrderDetailListByNo(orderNo);
			// orderItemVoList
			for (int j = 0; j < orderDetailList.size(); j++) {
				OrderDetail orderDetail = orderDetailList.get(j);
				AssistOrderItem assistOrderItem = new AssistOrderItem();
				assistOrderItem.setOrderNo(orderDetail.getOrder().getNo());
				assistOrderItem.setProductId(orderDetail.getProduct().getId());
				assistOrderItem.setSubTitle(orderDetail.getProduct().getSubtitle());
				assistOrderItem.setProductName(orderDetail.getCurrentProductName());
				assistOrderItem.setProductImage(orderDetail.getProduct().getMainImage());
				assistOrderItem.setCurrentUnitPrice(orderDetail.getCurrentUnitPrice());
				assistOrderItem.setQuantity(orderDetail.getQuantity());
				assistOrderItem.setTotalPrice(BigDecimal
						.valueOf(orderDetail.getQuantity() * orderDetail.getCurrentUnitPrice().doubleValue()));
				assistOrderItem.setCreateTime(orderDetail.getOrder().getCreateTime());
				// 添加订单详情，组成list
				assistOrderItems.add(assistOrderItem);
			}

			Order order = orderService.findOrderByNo(orderNo);
			AssistOrder assistOrder = new AssistOrder();
			assistOrder.setOrderNo(order.getNo());
			assistOrder.setPayment(order.getPayment());
			assistOrder.setPaymentTime(order.getCreateTime());
			assistOrder.setSendTime(order.getCreateTime());
			assistOrder.setEndTime(order.getEndTime());
			assistOrder.setCloseTime(order.getCloseTime());
			assistOrder.setCreateTime(order.getCreateTime());
			assistOrder.setImageHost(AndroidMalConstant.HOST_ADDRESS);
			assistOrder.setShippingId(order.getShipping().getId());

			// Shipping
			Shipping shipping = order.getShipping();
			AssistShipping assistShipping = new AssistShipping();
			assistShipping.setReceiverAddress(shipping.getAddress());
			assistShipping.setReceiverCity(shipping.getCity());
			assistShipping.setReceiverPhone(shipping.getPhone());
			assistShipping.setReceiverMobile(shipping.getMobile());
			assistShipping.setReceiverProvince(shipping.getProvince());
			assistShipping.setReceiverZip(shipping.getZip());
			// 添加ShippingVo
			assistOrder.setShippingVo(assistShipping);

			assistOrder.setReceiverName(order.getShipping().getName());
			assistOrder.setOrderItems(assistOrderItems);

			data.put("order", assistOrder);
			orderListData.put("data", data);
		} catch (NullPointerException e) {
			e.printStackTrace();
			orderListData.put("status", 1);
			orderListData.put("msg", "没登录");
		} finally {
			ResponseJsonUtils.json(response, orderListData);
		}
	}

	/**
	 * 
	 * @Title sendGoods  
	 * @Description TODO(发货，待续……)  
	 * @param @param orderNo
	 * @param @param response
	 * @return void
	 * @throws
	 */
	@RequestMapping(value = "/send_goods")
	public void sendGoods(String orderNo, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			// catch测试
			Order order = orderService.findOrderByNo(orderNo);
			order.getId();

			data.put("status", 0);
			data.put("data", "发货成功");
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "发货失败");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}
}
